﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace SoundServant.Controls
{
    /// <summary>
    /// Interaction logic for Track.xaml
    /// </summary>
    public partial class TrackReadOnlyControl : UserControl
    {
        StoredTrack track;
        public delegate void VoidInvokeHandler();

        public TrackReadOnlyControl(StoredTrack t)
        {
            InitializeComponent();

            track = t;
            Draw();
            if (track.Current)
            {
                SS.TenthOfASecondTimer.Tick += new EventHandler(Timer_Tick);
                track.Finished += new StoredTrack.VoidEventHandler(track_Finished);
            }
            else if (track.Error)
            {
                Time.Content = "Error";
                Title.Content = track.ErrorMessage;
            }

            track.Updated += new StoredTrack.VoidEventHandler(track_Updated);
        }

        void track_Finished()
        {
            SS.TenthOfASecondTimer.Tick -= Timer_Tick;
            track.Finished -= track_Finished;

            Draw();
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            Time.Content = SS.ParseTimeSpan(track.Length);
        }

        void track_Updated()
        {
            Draw();
        }

        private void Draw()
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (VoidInvokeHandler)delegate()
            {
            Title.Content = track.Title;
            Time.Content = SS.ParseTimeSpan(track.Length);
            Number.Content = "TRACK " + track.Number.ToString("00");
            });
        }
    }
}
